package cn.edu.cjw.APMFP.ui.panel;

import cn.edu.cjw.APMFP.ProductionTask.Service.TaskService;
import cn.edu.cjw.APMFP.ProductionTask.Service.TaskServiceImpl;
import cn.edu.cjw.APMFP.ProductionTask.pojo.ProductionTask;
import cn.edu.cjw.APMFP.Util.StringUtil;
import cn.edu.cjw.APMFP.ui.CenterPanel;
import cn.edu.cjw.APMFP.ui.component.PlaceholderTextField;
import cn.edu.cjw.APMFP.ui.dialog.ProductionTaskDialog;
import cn.edu.cjw.APMFP.ui.table.ProductionTaskTableModel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ProductionTaskPanel extends JPanel {

    private PlaceholderTextField searchField = new PlaceholderTextField(20);

    private JButton searchBtn = new JButton("查询");

    private JButton addBtn = new JButton("新增");

    private JButton editBtn = new JButton("修改");

    private JButton delBtn = new JButton("删除");

    private TaskService service = new TaskServiceImpl();

    private JPanel northPanel = new JPanel();

    private JScrollPane centerPanel = new JScrollPane();

    private JTable table = new JTable();

    private List<ProductionTask> list = new ArrayList<>();

    private CenterPanel mainPanel;

    private int index;


    public ProductionTaskPanel(CenterPanel mainPanel, int index){
        this.mainPanel = mainPanel;
        this.index = index;
        init();
    }

    private void init(){
        this.setLayout(new BorderLayout());
        searchField.setPlaceholder("任务单号");
        northPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        northPanel.add(searchField);
        northPanel.add(searchBtn);
        if(index == 0){
            northPanel.add(addBtn);
            northPanel.add(editBtn);
            northPanel.add(delBtn);
        }
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.getTableHeader().setReorderingAllowed(false);
        initTable();
        centerPanel.setViewportView(table);

        searchBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                initTable();
            }
        });

        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new ProductionTaskDialog(null, () -> {
                    initTable();
                });
            }
        });

        editBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = table.getSelectedRow();
                if(row == -1){
                    JOptionPane.showMessageDialog(null, "请选择修改行");
                    return;
                }
                new ProductionTaskDialog(list.get(row), () -> {
                    initTable();
                });
            }
        });

        delBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = table.getSelectedRow();
                if(row == -1){
                    JOptionPane.showMessageDialog(null, "请选择修改行");
                    return;
                }
                try {
                    service.removeTask(list.get(row));
                    JOptionPane.showMessageDialog(null, "删除成功");
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, "删除失败");
                }
            }
        });

        this.add(northPanel, BorderLayout.NORTH);
        this.add(centerPanel, BorderLayout.CENTER);
    }

    public void initTable(){
        String searchTxt = searchField.getText().trim();
        this.list.clear();
        try {
            if(StringUtil.isNotBlank(searchTxt)){
                this.list.addAll(Collections.singletonList(service.searchTaskById(searchTxt)));
            }else{
                if(index == 0){
                    this.list.addAll(service.showAll());
                }else if(index == 1){
                    this.list.addAll(service.searchTaskByIng());
                }else if(index == 2){
                    this.list.addAll(service.searchTaskByExamine());
                }else{
                    this.list.addAll(service.searchTaskComplete());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        ProductionTaskTableModel model = new ProductionTaskTableModel(list);
        table.setModel(model);
        model.fireTableDataChanged();
    }
}
